using System;
using System.Collections.Generic;
using Gameplay.PVE.Entity;
using Gameplay.PVE.Utils;
using UnityEngine;
using Yoozoo.Gameplay.RTS;
using Random = System.Random;

namespace Gameplay.PVE
{
    public class BattleUnitRoadPath : UnitRoadPath
    {
        private Vector3 lastTargetPosition;
        private float lastUpdateRoadPathTime = 0;
        private List<Vector3> pathList = new List<Vector3>();
        private int nextIndex = 0;
        private GameObject obj;
        public bool needRepath = false;
        private float lastRepathTime;
        private int currentFrame;

        private float reactionTime;
        private float startReactionTime;
        private Vector3 velocity;
        private Vector3 lastFollowVelocity;
        private Vector3 lastFollowPosition;
        private Vector3 lastFollowForward;

        private Vector3 lastRepathTarget;
        
   

        private bool isFindPathSuccess = false;

        public bool needShowPath = false;

        public override void Init(UnitBase unit)
        {
            base.Init(unit);
            actionName = "Run";
            actionSpeed = 1f;
            currentFrame = 0;
        }
        
        public override void OnTransmit(ETransmitType type,BaseTransmitArg arg)
        {
            base.OnTransmit(type,arg);
            if (type == ETransmitType.RePath)
            {
                needRepath = true;
            }
        }

        public override void OnDead()
        {
            HidePath();
        }
        
        private void UpdateNormalRpg()
        {
            var targetPosition = unit.Data.targetPosition;
            if (true)
            {
                bool isForceEnd = false;
                if (unit.Data.lockTarget == null || unit.Data.lockTarget.Data.isDead)
                {
                    targetPosition = unit.Data.targetPosition;
                    unit.Data.SetTargetForward(unit.Data.forward);
                    isForceEnd = true;
                }
                else
                {
                    if (unit.Data.attackPosition == Vector3.zero)
                    {
                        if (unit.Data.IsAttackInRange())
                        {
                            targetPosition = unit.Data.position;
                        }
                        else
                        {
                            targetPosition = unit.Data.lockTarget.Data.position;
                        }
                    }
                    else
                    {
                        targetPosition = unit.Data.attackPosition;
                        isForceEnd = true;
                    }
                }

                bool canRepath = lastRepathTarget != targetPosition && (!isFindPathSuccess && TimeManager.logicTime - lastRepathTime > 0.5f);
                if (canRepath || needRepath)
                {
                    if (unit.Data.modelId == 9999)
                    {
                        pathList.Clear();
                        pathList.Add(unit.Data.position);
                        pathList.Add(targetPosition);
                    }
                    else
                    {
                        UpdateAStarPath(unit.Data.position, targetPosition,isForceEnd);
                    }

                    
                    if (needShowPath)
                    {
                        ShowPath();
                        needShowPath = false;
                    }

                    if (pathList.Count > 0)
                    {
                        lastRepathTarget = pathList[pathList.Count - 1];
                        //PveFindPathManager.Instance.UpdateSingleCollider(unit.Data.id,pathList[pathList.Count - 1]);
                    }
                    else
                    {
                        //PveFindPathManager.Instance.UpdateSingleCollider(unit.Data.id,targetPosition);
                        lastRepathTarget = targetPosition;
                    }
                    
                    //unit.Data.attackPosition = lastRepathTarget;
                    lastRepathTime = TimeManager.logicTime;
                    needRepath = false;
                }
                //lastRepathTarget = targetPosition;
            }
            UpdateRoadPath();
        }

   
        public override void Update()
        {
            base.Update();
            
            UpdateNormalRpg();
            if (position == unit.Data.position)
            {
                unit.Data.targetMoveSpeed = 0;
                unit.Data.moveState = 0;
                isFinish = true;
            }
        }

    
        protected override void Reset()
        {
            base.Reset();
            pathList.Clear();
            needRepath = false;
            lastRepathTime = 0;
        }
        
    }
}